(function() {

var Textbox = function(label, color, fontsize, bgsource) {
  this.initialize(label, color, fontsize, bgsource);
};
var p = Textbox.prototype = new createjs.Container(); // inherit from Container


this.text;
p.label;
this.bg_succ;
this.bg;
this.dragger;

this.width;
this.height;

p.Container_initialize = p.initialize;
p.initialize = function(label, color, fontsize, bgsource) {
	this.Container_initialize();
	
	this.label = label;
	if (!color) { color = "#CCC"; }
	
	this.text = new createjs.Text(label, fontsize + "px Arial", color);
	this.text.textBaseline = "top";
	this.text.textAlign = "left";
        this.text.lineWidth = stage.canvas.width - 100;
	
	this.width = this.text.getMeasuredWidth()+30;
	this.height = this.text.getMeasuredHeight()+20;
	
	this.text.x = 10;
	this.text.y = 10;
	
	this.bg_succ = new createjs.Bitmap(queue.getResult("bgTextSuccess"));
	this.bg_succ.x = this.bg_succ.y = 0;
        this.bgsuccesssize(this.width, this.height);
	
        this.bg = new createjs.Bitmap(queue.getResult(bgsource));
        this.bg.x = this.bg.y = 0;
        this.bgsize(this.width, this.height);
        
        this.dragger = new createjs.Container();
        this.dragger.x = this.dragger.y = 100;
        this.dragger.addChild(this.bg, this.text);
        stage.addChild(this.dragger);
	
	this.addChild(this.dragger); 
	this.on("click", this.handleClick);
	this.on("tick", this.handleTick);

	this.mouseChildren = false;
};

p.removeChild = function() {
        
                
};

p.bgsize = function(windowWidth, windowHeight) {
        var bounds = this.bg.getBounds();
        var currentWidth = bounds.width;
        var currentHeight = bounds.height;

        this.bg.scaleX = windowWidth / currentWidth;
        this.bg.scaleY = windowHeight / currentHeight;
                
};

p.bgsuccesssize = function(windowWidth, windowHeight) {
        var bounds = this.bg_succ.getBounds();
        var currentWidth = bounds.width;
        var currentHeight = bounds.height;

        this.bg_succ.scaleX = windowWidth / currentWidth;
        this.bg_succ.scaleY = windowHeight / currentHeight;
                
};

p.succeed = function() {
        this.dragger.removeChild(this.bg, this.text);
        this.dragger.addChild(this.bg_succ, this.text);
};

p.appearLine = function(fromX, fromY, moveX, moveY) {
//        var bounds = p.bmp.getBounds();
        //move the form above the screen
        this.dragger.x = fromX;
        this.dragger.y = fromY;
        createjs.Tween.get(this.dragger).to({alpha: 1, x: moveX, y: moveY}, 2000, createjs.Ease.linear);
};

p.hitTest = function(mouseX, mouseY) {
//        var bounds = p.bmp.getBounds();
        return this.dragger.x <= mouseX && mouseX <= this.dragger.x + this.width
            && this.dragger.y <= mouseY && mouseY <= this.dragger.y + this.height;
};

p.position = function(x, y) {
        this.dragger.x = x;
        this.dragger.y = y;
};

p.handleClick = function (event) {    
//	var target = event.target;
//	alert("You clicked on a button: "+ target.label);
};

p.handleTick = function(event) {       
//	p.alpha = Math.cos(p.count++*0.1)*0.4+0.6;
};

window.Textbox = Textbox;
}());